﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WindowsForms.AOnline.Presenter.Dictionary.Unit;
using WindowsForms.AOnline.View.Dictionary;
using WindowsForms.AOnline.WindowsForm.FormBase;
using WindowsForms.AOnline.WindowsForm.Resources;

namespace WindowsForms.AOnline.WindowsForm.FormDictionary
{
    public partial class FrmXtraUnitDetail : FrmXtraBaseTreeDetail, IUnitView, IUnitsView
    {
        private readonly UnitPresenter _unitPresenter;
        private readonly UnitsPresenter _unitsPresenter;

        public FrmXtraUnitDetail()
        {
            InitializeComponent();
            _unitPresenter = new UnitPresenter(this);
            _unitsPresenter = new UnitsPresenter(this);
        }

        /// <summary>
        /// Initializes the data.
        /// </summary>
        protected override void InitData()
        {
            if (KeyValue != null)
            {
                _unitPresenter.Display(KeyValue);
              //  _unitsPresenter.DisplayForComboTree(int.Parse(KeyValue));
                _unitsPresenter.GetAll();
                grdLockUpParentId.Enabled = false;
            }
            else
            {
                //   _unitsPresenter.Display(cu);
                _unitsPresenter.GetAll();
            }
        }

        /// <summary>
        /// Focuses the control.
        /// </summary>
        protected override void InitControls()
        {
            txtUnitCode.Focus();
            if ((ActionMode == ActionModeEnum.Edit) && HasChildren)
                grdLockUpParentId.Properties.ReadOnly = true;
            else
                grdLockUpParentId.Properties.ReadOnly = false;
        }

        /// <summary>
        /// Valids the data.
        /// </summary>
        /// <returns></returns>
        protected override bool ValidData()
        {
            if (string.IsNullOrEmpty(Code))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResUnitCode"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUnitCode.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(UnitName))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResUnitName"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUnitName.Focus();
                return false;
            }

            if (Code == grdLockUpParentId.Text)
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResCodeSameAsParentError"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                grdLockUpParentId.Focus();
                return false;
            }

            return true;
        }

        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <returns></returns>
        protected override string SaveData() 
        {
            return _unitPresenter.Save(); 
        }



        #region IUnitView Members
        public int UnitId
        {
            get;

            set;
        }

        public int? ParentId
        {
            get
            {
                if (grdLockUpParentId.EditValue == null) return null;
                return (int?)grdLockUpParentId.GetColumnValue(KeyFieldName);
            }
            set
            {
                grdLockUpParentId.EditValue = value;
            }
        }

        public int Grade
        {
            get;

            set;
        }

        public bool HasParent
        {
            get;

            set;
        }

        public string PhoneNumber
        {
            get { return txtPhone.Text; }
            set { txtPhone.Text = value; }
        }

        public string UnitName
        {
            get { return txtUnitName.Text; }
            set { txtUnitName.Text = value; }
        }

        public string Code
        {
            get { return txtUnitCode.Text; }
            set { txtUnitCode.Text = value; }
        }

        public string Address
        {
            get { return txtAddress.Text; }
            set { txtAddress.Text = value; }
        }

        public bool IsActive
        {
            get { return chkIsActive.Checked; }
            set { chkIsActive.Checked = value; }
        }

        public string ByManagement
        {
            get { return txtManagement.Text; }
            set { txtManagement.Text = value; }
        }
        #endregion

        #region IUnitsView Members
        public IList<Model.BusinessObjects.Dictionary.UnitModel> Units
        {
            set
            {
                grdLockUpParentId.Properties.DataSource = value;
                grdLockUpParentId.Properties.ForceInitialize();
                grdLockUpParentId.Properties.PopulateColumns();

                grdLockUpParentId.Properties.DisplayMember = "Code";
                grdLockUpParentId.Properties.ValueMember = KeyFieldName;
            }
        }
        #endregion


      
    }
}